const md5 = require('md5')
const jwt = require('jsonwebtoken')
const { loginDao, insertAuditRecordDao } = require('../dao/adminDao')
const { getSupplierListDao, getSupplierCountDao, updateSupplierStateDao } = require('../dao/webDao')


exports.loginService = async function(info) {
    let data = await loginDao(info)
    let token = ''
    if (data) {
        // 添加token
        token = jwt.sign({
            _id: data._id,
            a_name: data.a_name,
            a_email: data.a_email,
            a_telphone: data.a_telphone
        }, md5(process.env.JWT_SECRET), { expiresIn: 60*60*24})
    }
    return { data, token }
}

exports.getUserListService = async function(info) {
    const list = await getSupplierListDao(info)
    const total = await getSupplierCountDao(info)
    return { list, total }
}

exports.userActionService = async function(info, user) {
    // 修改用户状态
    await updateSupplierStateDao(info)
    // 插入审核记录
    const list = await insertAuditRecordDao(info, user)
    return list
}
